<?php


namespace app\erp\model;


use think\Db;
use think\Model;

class Dianzidb extends Model
{
    protected $name = 'dianzi';
    protected $pk = 'id';

    public $param = [];

    protected $append = ['weishoukuan', 'funded_money', 'callect_money'];

    public function base($query)
    {
        // 搜索状态
        if (isset($this->param['apply_state']) && $this->param['apply_state'] != '') {
            $query->where('apply_state', $this->param['apply_state']);
        }

        // 搜索军订单号
        if (isset($this->param['out_order_no']) && $this->param['out_order_no']) {
            if (strlen($this->param['out_order_no']) == 20) {
                $query->where('out_order_no', $this->param['out_order_no']);
            } else {
                $query->whereLike('out_order_no', '%' . $this->param['out_order_no'] . '%');
            }
        }

        // 搜索订单号

        if (isset($this->param['order_no']) && $this->param['order_no']) {
            if (strlen($this->param['order_no']) == 22) {
                $query->where('order_no', $this->param['order_no']);
            } else {
                $query->whereLike('order_no', '%' . $this->param['order_no'] . '%');
            }
        }
        //订单时间搜索
        if (isset($this->param['create_time_start']) && $this->param['create_time_start']) {
            $query->whereTime('order_time', '>=', $this->param['create_time_start']);
        }
        if (isset($this->param['order_time_end']) && $this->param['order_time_end']) {
            $time = strtotime($this->param['order_time_end']);
            $date = strtotime('+1 day', $time);
            $query->whereTime('order_time', '<', $date);
        }
        //申请时间搜搜
        if (isset($this->param['apply_time_start']) && $this->param['apply_time_start']) {
            $query->whereTime('apply_time', '>=', $this->param['apply_time_start']);
        }
        if (isset($this->param['apply_time_end']) && $this->param['apply_time_end']) {
            $time = strtotime($this->param['apply_time_end']);
            $date = strtotime('+1 day', $time);
            $query->whereTime('apply_time', '<', $date);
        }
        if (isset($this->param['audit_yestoday']) && $this->param['audit_yestoday'] == 'yes') {
            $query->whereTime('audit_time', 'yesterday');
        }
        //审核时间搜搜
        if (isset($this->param['audit_time_start']) && $this->param['audit_time_start']) {
            $query->whereTime('audit_time', '>=', $this->param['audit_time_start']);
        }
        if (isset($this->param['audit_time_end']) && $this->param['audit_time_end']) {
            $time = strtotime($this->param['audit_time_end']);
            $date = strtotime('+1 day', $time);
            $query->whereTime('audit_time', '<', $date);
        }
    }

    public function supplier()
    {
        return $this->belongsTo(Memberdb::class, 'supplier_id', 'member_id');
    }

    public function auditer()
    {
        return $this->belongsTo(Memberdb::class, 'audit_member_id', 'member_id')->bind(['username']);
    }

// 查询订单商品信息
    protected function getCart($order_no)
    {
        return Db::name('cart')->where('order_no', $order_no)->field('goods_price,goods_num')->select();
    }

    // 销售金额
    public function getSaleMoneyAttr($val, $row)
    {
        $data = $this->getCart($row['order_no']);
        $result = 0;
        foreach ($data as $key => $val) {
            $result += round($val['goods_price'] * 0.97 * 100) / 100 * $val['goods_num'];
        }
        return number_format($result, 2, '.', '');
        //return number_format($row['order_money'] * 0.97, 2, '.', '');
    }

    // 采购金额
    public function getCallectMoneyAttr($val, $row)
    {
        $data = $this->getCart($row['order_no']);
        $result = 0;
        foreach ($data as $key => $val) {
            $result += round($val['goods_price'] * 0.966 * 100) / 100 * $val['goods_num'];
        }
        return number_format($result, 2, '.', '');

        //return number_format($row['order_money'] * 0.966, 2, '.', '');
    }

    // 代采金额
    public function getFundedMoneyAttr($val, $row)
    {
        $data = $this->getCart($row['order_no']);
        $result = 0;
        foreach ($data as $key => $val) {
            $result += round($val['goods_price'] * 0.966 * 100) / 100 * $val['goods_num'];
        }
        $money = floor($result*0.9*100)/100;
        return number_format($money, 2, '.', '');

        //return number_format($row['order_money'] * 0.966 * 0.9 , 2, '.', '');
    }

    /**
     * Title("供应商应收款:365天内所有未收款")
     */
    public function getWeishoukuanAttr($val, $row)
    {
        $orderdb = new Orderdb();
        $get_money = $orderdb
            ->where('supplier_er', $row['supplier_id'])
            ->whereTime('create_time', '>=', strtotime('-1 year'))
            ->where('pay_state', '<>', 2)
            ->sum('order_money');
        return number_format($get_money / 100, 2, '.', '');
    }

    /**
     * Title("关联购物车模型")
     */
    public function cart()
    {
        return $this->hasMany(Cartdb::class, 'order_no', 'order_no');
    }

    /**
     * Title("关联订单表")
     */
    public function orders()
    {
        return $this->belongsTo(Orderdb::class, 'order_no', 'order_no');
    }

    /**
     * Title("获取供应商名称")
     */
    public function getCompanyNameAttr($val, $row)
    {
        return Db::name('member')->where('member_id', $row['supplier_id'])->value('company_name');
    }

}